
//
//  WZXFindDetailViewController.m
//  WWLNetease
//
//  Created by dllo on 15/12/29.
//  Copyright © 2015年 Echo.Jobs. All rights reserved.
//

#import "WZXFindDetailViewController.h"
#import "WZXFindOneDetailTableViewCell.h"
#import "WZXFindDetailModel.h"
#import "WZXSectionView.h"
#import "WZXQuextionModel.h"
#import "WZXAnswerModel.h"



@interface WZXFindDetailViewController ()<UITableViewDataSource,UITableViewDelegate>

//创建tableView
@property (nonatomic ,strong)UITableView *myTableView;
//大图片
@property (nonatomic ,strong)UIImageView *bigImageV;
//大图片上的解析
@property (nonatomic ,strong)UILabel *aliasLabel;
//大图片上的关注数
@property (nonatomic ,strong)UILabel *countLabel;
//小网易
@property (nonatomic ,strong)UIImageView *smallWYV;
//专家头像
@property (nonatomic ,strong)UIImageView *headerImageV;
//专家名字
@property (nonatomic ,strong)UILabel *nameLabel;
//专家职业
@property (nonatomic ,strong)UILabel *jobLabel;
//大图片上的数组
@property (nonatomic ,strong)NSMutableArray *FirstAllArray;
//大图片下面的问答的大组数
@property (nonatomic ,strong)NSMutableArray *SecondAllArray;

@property (nonatomic ,strong)WZXFindDetailModel *model;

@property (nonatomic ,strong)NSMutableArray *quesArray;

@end

@implementation WZXFindDetailViewController

- (void)viewWillAppear:(BOOL)animated {
    
    self.navigationController.navigationBar.hidden = YES;
    self.tabBarController.tabBar.hidden = YES;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
#pragma mark - 初始化数组
    self.FirstAllArray = [NSMutableArray array];
    self.SecondAllArray = [NSMutableArray array];
    self.quesArray = [NSMutableArray array];
    
    
    // Do any additional setup after loading the view.
#pragma mark - imageView 不随滑动而改变
//    [self setAutomaticallyAdjustsScrollViewInsets:NO];
#pragma mark - 创建大图片上的视图
    [self createSubView];
#pragma mark - 右边的button的分享功能
    [self createRightButton];
#pragma mark - 左边的button返回功能
    [self createLeftButton];
#pragma mark - 注册cell
    [self.myTableView registerClass:[WZXFindOneDetailTableViewCell class] forCellReuseIdentifier:@"Cell"];
#pragma mark - getData
    [self getData];
    
}

- (void)createSubView {
    
#pragma mark - 创建tableView
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    self.myTableView.contentInset = UIEdgeInsetsMake(250 * SIZE, 0, 0, 0);
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    
#pragma mark - 创建大图片
//    self.bigImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"u=1005507773,1778153223&fm=21&gp=0.jpg"]];
    self.bigImageV = [[UIImageView alloc] init];
    self.bigImageV.frame = CGRectMake(0, -250 * SIZE, 375 * SIZE, 250 * SIZE);
    self.bigImageV.backgroundColor = [UIColor lightGrayColor];
    self.bigImageV.contentMode = UIViewContentModeScaleAspectFill;
    [self.myTableView addSubview:self.bigImageV];
    
#pragma mark - 创建大图片上的label
    self.aliasLabel = [[UILabel alloc] initWithFrame:CGRectMake(30 * SIZE, 70 * SIZE, 315 * SIZE, 60 * SIZE)];
    self.aliasLabel.numberOfLines = 0;
    self.aliasLabel.textAlignment = NSTextAlignmentCenter;
    self.aliasLabel.textColor = [UIColor whiteColor];
    self.aliasLabel.shadowColor = [UIColor blackColor];
    [self.bigImageV addSubview:self.aliasLabel];
    
#pragma mark - 大图片上的关注数
    self.countLabel = [[UILabel alloc] initWithFrame:CGRectMake(70 * SIZE, 125 * SIZE, 235 * SIZE, 30 * SIZE)];
    self.countLabel.textAlignment = NSTextAlignmentCenter;
    self.countLabel.shadowColor = [UIColor blackColor];
    self.countLabel.textColor = [UIColor whiteColor];
//    self.countLabel.text = @"一 1154关注 一";
    [self.bigImageV addSubview:self.countLabel];
    
#pragma mark - 网易小图片
    self.smallWYV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navbar_netease@2x.png"]];
    self.smallWYV.frame = CGRectMake(157 * SIZE, 190 * SIZE, 60 * SIZE, 30 * SIZE);
    [self.bigImageV addSubview:self.smallWYV];
    
#pragma mark - 专家头像
    self.headerImageV = [[UIImageView alloc] init];
    self.headerImageV.frame = CGRectMake(10 * SIZE, 220 * SIZE, 50 * SIZE, 50 * SIZE);
    self.headerImageV.layer.masksToBounds = YES;//裁掉边界
    self.headerImageV.layer.cornerRadius = 25 * SIZE;//设置半径
//    self.headerImageV.backgroundColor = [UIColor redColor];
    [self.bigImageV addSubview:self.headerImageV];
    
#pragma mark - 专家名字
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(65 * SIZE, 230 * SIZE, 60 * SIZE, 30 * SIZE)];
    self.nameLabel.font = [UIFont systemFontOfSize:14];
    self.nameLabel.textAlignment = NSTextAlignmentLeft;
    self.nameLabel.textColor = [UIColor whiteColor];
    self.nameLabel.shadowColor = [UIColor blackColor];
    [self.bigImageV addSubview:self.nameLabel];
    
#pragma mark - 专家职业
    self.jobLabel = [[UILabel alloc] initWithFrame:CGRectMake(130 * SIZE, 230 * SIZE, 200 * SIZE, 30 * SIZE)];
    self.jobLabel.font = [UIFont systemFontOfSize:14];
    self.jobLabel.textColor = [UIColor whiteColor];
    self.jobLabel.shadowColor = [UIColor blackColor];
    [self.bigImageV addSubview:self.jobLabel];

}


#pragma mark - 解析数据
- (void)getData {
    
    NSString *urlStr = [NSString stringWithFormat:@"http://c.m.163.com/newstopic/qa/%@.html",self.expertID];
    [AppTools getDataWithURL:urlStr Block:^(id result) {
        
        NSMutableDictionary *bigDic = result;
        NSMutableDictionary *dataDic = [bigDic objectForKey:@"data"];
        NSMutableDictionary *expertDic = [dataDic objectForKey:@"expert"];
        NSMutableArray *hotlistArr = [dataDic objectForKey:@"hotList"];
        
        
            self.model = [[WZXFindDetailModel alloc] init];
            [self.model setValuesForKeysWithDictionary:expertDic];
            [self.FirstAllArray addObject:self.model];
        
        for (NSMutableDictionary *dic in hotlistArr) {
            
            NSMutableDictionary *quesDic = [dic objectForKey:@"question"];
            
            /* 取出问题字 返回自适应高度 */
            NSString *content = [quesDic objectForKey:@"content"];
            CGFloat cellHeight = [AdjustLabelWidthToFitFontAppTools heightForContent:content width:320 fontSize:14];
            
            WZXQuextionModel *quesModel = [[WZXQuextionModel alloc] init];
            [quesModel setValuesForKeysWithDictionary:quesDic];
            
            quesModel.height = cellHeight;
            
            [self.quesArray addObject:quesModel];
            
           
        }
        


        
        [self sendValue];

        
        [self.myTableView reloadData];
    } ErrorBlock:^(NSError *error) {
        
        
        NSLog(@"请求失败");
        
    }];
 
}

- (void)sendValue {

    
        [self.bigImageV sd_setImageWithURL:[NSURL URLWithString:self.model.picurl] placeholderImage:nil];
        self.aliasLabel.text = self.model.alias;
        [self.headerImageV sd_setImageWithURL:[NSURL URLWithString:self.model.headpicurl] placeholderImage:nil];
        NSString *str = self.model.concernCount;
        self.countLabel.text = [NSString stringWithFormat:@"一 %@关注 一",str];
        self.nameLabel.text = self.model.name;
        self.jobLabel.text = self.model.title;
    
}



#pragma mark - 创建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WZXFindOneDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
    cell.quesModel = [self.quesArray objectAtIndex:indexPath.row];
    
    NSLog(@"------%@",cell.quesModel.content);
    return cell;
}

#pragma mark - 两个必要的方法
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSLog(@"-------%ld",self.quesArray.count);
    return self.quesArray.count;
    
}

#pragma mark - 分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
    
}

#pragma mark - 分区头的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 35 * SIZE;
    
}

#pragma mark - cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WZXQuextionModel *quesHModel = [self.quesArray objectAtIndex:indexPath.row];
    
    return quesHModel.height + 50;
    
}




#pragma mark - 自定义section方法,铺个View,在View上铺label
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
 /* 创建View */
    WZXSectionView *sectionView = [[WZXSectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30 * SIZE)];
    sectionView.backgroundColor = [UIColor colorWithWhite:0.941 alpha:1.000];
    
#pragma mark - 给sectionView上的label赋值
    NSString *str1 = self.model.questionCount;
    sectionView.questionLabel.text = [NSString stringWithFormat:@"%@提问",str1];
    NSString *str2 = self.model.answerCount;
    sectionView.answerLabel.text = [NSString stringWithFormat:@"%@回答",str2];
    
    
    return sectionView;
    
}

#pragma mark - 左上角的button
- (void)createLeftButton
{
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(22 * SIZE, 22 * SIZE, 42 * SIZE, 42 * SIZE);
    [leftButton setImage:[UIImage imageNamed:@"iconfont-zuojiantouquan.png"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftButton];
    
}

#pragma mark - 右上角的button
- (void)createRightButton
{
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake((367 - 54) * SIZE, 22 * SIZE, 42 * SIZE, 42 * SIZE);
    [rightButton setImage:[UIImage imageNamed:@"iconfont-fenxiang.png"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(rightButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:rightButton];
    
}

#pragma mark - 效果的方法的实现
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //刚开始y的偏移量初始值就是-264
//    NSLog(@"y1 === %f",scrollView.contentOffset.y);
    CGFloat y = scrollView.contentOffset.y;//加上导航栏高度,第一次是-200
//    NSLog(@"y2 === %f",y);
    
    if (y < -250 * SIZE) {
        CGRect frame = self.bigImageV.frame;
        frame.origin.y = y;//偏移了多少,
        frame.size.height =  -y;//tablview向下偏移了多少,高度就是多少
        self.bigImageV.frame = frame;
    }
    
}


#pragma mark - 左侧按钮方法
- (void)leftButtonAction:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - 右侧按钮方法
- (void)rightButtonAction:(UIButton *)btn
{
    
//    [UMSocialSnsService presentSnsIconSheetView:self
//                                         appKey:@"507fcab25270157b37000010"
//                                      shareText:@"你要分享的文字"
//                                     shareImage:[UIImage imageNamed:@"`"]
//                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToWechatSession,UMShareToQQ,UMShareToRenren,nil]
//                                       delegate:self];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
